<template>
  <div>
    <el-drawer
      :title="dialogType==0?'添加':'编辑'"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
    >
      <el-form
        resetFields
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="套餐名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>

        <el-form-item label="套餐类型" prop="type">
          <el-select v-model="ruleForm.type" placeholder="请选择套餐类型">
            <el-option label="老年人套餐" :value="0"></el-option>
            <el-option label="儿童套餐" :value="1"></el-option>
            <el-option label="通用套餐" :value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="活动区域" prop="region">
          <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
            <el-option label="东院区" :value="0"></el-option>
            <el-option label="西院区" :value="1"></el-option>
            <el-option label="北院区" :value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="套餐价格" prop="price">
          <el-input v-model.number="ruleForm.price"></el-input>
        </el-form-item>

        <el-form-item label="是否可用" prop="state">
          <el-switch v-model="ruleForm.state"></el-switch>
        </el-form-item>

        <el-form-item label="套餐项目" prop="project">
          <el-input type="textarea" v-model="ruleForm.project"></el-input>
        </el-form-item>

        <el-form-item class="btn">
          <el-button type="primary" @click="submitForm('ruleForm')">{{dialogType==0?'添加':'编辑'}}</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import eventBus from "../../../utils/busEvent";
import { addData } from "../../../api/physicaAPI";
import { modifyData } from "../../../api/physicaAPI";
export default {
  mounted() {
    eventBus.$on("drawerTransfer", () => {
      this.ruleForm = {};
      this.dialogType = 0;
      this.drawer = true;
    });
    eventBus.$on("modifyTransfer", row => {
      this.dialogType = 1;
      this.drawer = true;
      this.ruleForm.ID = row.ID;
      this.ruleForm.name = row.name;
      this.ruleForm.type = row.type;
      this.ruleForm.region = row.region;
      this.ruleForm.project = row.project;
      this.ruleForm.state = row.state;
      this.ruleForm.price = row.price;
    });
  },

  data() {
    return {
      drawer: false,
      direction: "ltr",
      ruleForm: {
        ID: "",
        name: "",
        type: "",
        region: "",
        price: "",
        project: "",
        state: false
      },
      dialogType: 1,
      rules: {
        name: [{ required: true, message: "请输入套餐类型", trigger: "blur" }],
        // type: [
        //   { required: true, message: "请选择套餐类型", trigger: "change" }
        // ],
        // region: [{ required: true, message: "请选择院区", trigger: "change" }],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],
        project: [{ required: true, message: "请输入项目", trigger: "blur" }]
      }
    };
  },
  methods: {
    resetForm(formName) {
      this.ruleForm = {};
      this.$refs[formName].resetFields();
    },
    handleClose(done) {
      done();
    },
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.dialogType == 0) {
            addData(this.ruleForm).then(res => {
              if (res.code == 200) {
                this.$emit("addDataTransfer");
              }
            });
          } else {
            modifyData(this.ruleForm).then(res => {
              this.$emit("modifyDataTransfer");
            });
          }
          this.drawer = false;
         
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.el-form-item {
  width: 430px;
}
.btn {
  width: 300px;
  margin-left: 71px;
}
</style>

